package com.lwj.project1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lwj.project1.databaseHelper.MySqliteOpenHelper;
import com.lwj.project1.entity.Game;
import com.lwj.project1.entity.GameCategory;
import com.lwj.project1.myToast.MyToast;

public class AddGameActivity extends AppCompatActivity {

    private EditText gameName;
    private EditText gameDescription;
    private Button button;
    private Button refresh;
    private MySqliteOpenHelper mySqliteOpenHelper;
    private MyToast myToast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_game);

        gameName = findViewById(R.id.addGameName);
        gameDescription = findViewById(R.id.addGameDescription);
        button = findViewById(R.id.button4);
        refresh = findViewById(R.id.button6);
        mySqliteOpenHelper = new MySqliteOpenHelper(getApplicationContext());
        myToast = new MyToast(getApplicationContext());
        SharedPreferences sharedPreferences = getSharedPreferences("project_content",MODE_PRIVATE);
        String username = sharedPreferences.getString("username","");
        button.setOnClickListener(v -> {
            String game_name = gameName.getText().toString();
            String game_description = gameDescription.getText().toString();
            GameCategory gameCategory = mySqliteOpenHelper.findGameCategoryByName(game_name);
            if (gameCategory == null) {
                myToast.showToastWithImg("没有找到该桌游哦，是不是记错了呢？",getResources().getDrawable(R.drawable.toast_img));
                return;
            }
            Game game = new Game(game_description,username,gameCategory.getGameCategoryId());
            mySqliteOpenHelper.addGame(game);
            myToast.showToastWithImg("添加成功啦，快去你的仓库看看吧！",getResources().getDrawable(R.drawable.toast_img));
        });
        refresh.setOnClickListener(v -> {
            gameName.setText("");
            gameDescription.setText("");
        });
    }
}